using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TurbaconPhone
{
    public abstract class ParticleEmitter : GameObject
    {
        #region Fields
        protected GameObject Owner = null;

        Vector2 position;
        Vector2 velocity;

        float LifeTime;
        float TickLength;
        float TickCounter = 0;

        int Amount;
        #endregion

        #region Properties
        public override Vector2 Pos
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public override Vector2 Vel
        {
            get
            {
                return velocity;
            }
            set
            {
                velocity = value;
            }
        }

        public override float Angle
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override float AngVel
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override float Scale
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion

        public ParticleEmitter(string textureName, Vector2 position, Vector2 velocity, float lifeTime, float tickLength, int amount)
            : base(textureName)
        {
            Pos = position;
            Vel = velocity;

            LifeTime = lifeTime;
            TickLength = tickLength;

            Amount = amount;
        }

        public override void Update(GameTime gameTime)
        {
            if (TickCounter <= 0)
            {
                for (int i = 0; i < Amount; i++)
                {
                    CreateParticle();
                }
                TickCounter = TickLength;
            }

            LifeTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (LifeTime <= 0) Die();

            TickCounter -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (Owner != null)
            {
                Pos = Owner.Pos;
            }
            else
            Pos += Vel;

            
            base.Update(gameTime);
        }

        public void Attach(GameObject obj)
        {
            Owner = obj;
            Vel = Vector2.Zero;
        }

        public override void Draw(SpriteBatch spriteBatch)
        { }

        protected abstract void CreateParticle();
    }
}
